/**
 * 功能描述 餐饮落实
 */

define(function(require, exports, module) {
	var Dialog = require("dialog");
	var HandlerBars = require("handlebars");
	var moment = require("moment");
	var dinnerCalendar = require("../assets/tpl/dinner_calendar.tpl");
	var dinnerCalendarItem = require("../assets/tpl/dinner_calendar_item.tpl");
	var dinnerForm = require("../component/ciiDinnerForm");
	var checkinimplementSvc = require("../service/checkinimplementSvc");
	
	var instance = {
		initComponent : function(widget,data,flag) {
			var that = this;
			this.flag = flag;
			widget.pkCheckInDinner = null;
			that.pkCheckInDinner = null;
			that.pkCheckInImplement = data.pkCIImplement;
			if(data.checkInDinner){
				that.pkCheckInDinner = data.checkInDinner.pkCheckInDinner;
				widget.pkCheckInDinner = data.checkInDinner.pkCheckInDinner;
				
				this.getDinnersSvc(widget,data.checkInDinner.pkCheckInDinner,function(dinnersData){
					that.mainData = dinnersData;
					var tempDate = widget.get("ciiBaseForm").getValue("checkInDate");
					that.renderCalendar(widget,tempDate);
					that.setCalendarData(widget,tempDate,that.mainData);
				});
			}else{
				this.mainData = [];
				var tempDate = widget.get("ciiBaseForm").getValue("checkInDate");
				this.renderCalendar(widget,tempDate);
				this.setCalendarData(widget,tempDate,this.mainData);
			}
		},
		// 绘制日历框架
		renderCalendar : function(widget,tempDate) {
			
			var dateStr = moment(tempDate).format("YYYY-MM");
			var html = HandlerBars.compile(dinnerCalendar)({monthStr:dateStr});
			$("#checkInDinner").html(html);
			
			
			if(this.flag){
				$("#batch-ready-li").hide();
			}else{
				$("#batch-ready-li").show();
			}
			
			var startDate = tempDate;
			var idate = moment(startDate);
			var startDateWeek = idate.day();
			var monthDayNum = new Date(idate.year(), idate.month() + 1, 0).getDate();
			var allTdNum = parseInt((monthDayNum + startDateWeek) / 7 + 1) * 7;
			$(".J-calendar-table", widget.element).html("");

			// 绘制日历
			for (var i = 0; i < allTdNum; i++) {
				var trIndex = parseInt(i / 7);
				if (i % 7 == 0) {
					$(".J-calendar-table", widget.element).append(
							"<tr class='tr_" + trIndex + "'></tr>");
				}
				if (i > startDateWeek - 1 && i <= (monthDayNum + startDateWeek - 1)) {
					$($(".J-calendar-table tr", widget.element)[trIndex])
							.append("<td class='td_" + (i - startDateWeek + 1) + "'></td>");
				} else {
					$($(".J-calendar-table tr", widget.element)[trIndex]).append("<td></td>");
				}
			}
			
		},
		//设置日历数据
        setCalendarData:function(widget,tempDate,dinnerData){
        	var selectDate = tempDate;
        	
        	if(!selectDate){
        		return;
        	}
        	
        	var dateStr = moment(tempDate).format("YYYY-MM");
        	
        	var startDate = widget.get("ciiBaseForm").getValue("checkInDate");
        	var endDate = widget.get("ciiBaseForm").getValue("checkOutDate");
        	
        	var idate = moment(tempDate);
        	var monthDayNum = new Date(idate.year(),idate.month()+1,0).getDate();
        	
        	for(var j=1;j<=monthDayNum;j++){
        		var idate = dateStr+"-"+j;
        		var isInDate = moment(idate).isBetween(startDate, endDate);
        		var isStartSame = moment(idate).isSame(startDate);
        		var isEndSame = moment(idate).isSame(endDate);
        		
        		if(isInDate || isStartSame || isEndSame){
        			var html = HandlerBars.compile(dinnerCalendarItem)({
        						saleDate:j,
        						calendarDate:moment(idate).format("YYYYMMDD"),
        						dateOfDinner:moment(idate).format("YYYY-MM-DD")
        					});
        			
        			$(".td_"+j,widget.element).html(html);
        		}else{
        			$(".td_"+j,widget.element).html(j);
        		}
        	}
        	
        	var dinners = dinnerData;
        	for(var i=0;i<dinners.length;i++){
        		var dateOfDinner = moment(dinners[i].dateOfDinner).format("YYYYMMDD");
        		var tpDom = $("#"+dinners[i].dinnerSort.key+"_"+dateOfDinner);
        		if(dinners[i].isValid){
        			tpDom.attr("checked","true");
        		}
        		
        		tpDom.attr("data-pkDinner",dinners[i].pkDinner);
        		tpDom.attr("data-version",dinners[i].version);
        		
        		tpDom.parent().find(".dinnerDate").text(dinners[i].dinnerDate);
        	}
        	
        },
        getDinnersSvc:function(widget,pkCheckInDinner,sCb){
        	var cParams = {
				"checkInDinner.pkCheckInDinner":pkCheckInDinner,
				fetchProperties:"*",
			};
			checkinimplementSvc.getDinners(cParams,sCb);
        },
        dinnerCheckboxClick:function(e,widget,type){
        	var that = this;
        	
        	if(this.flag){
        		if(e.currentTarget.checked){
        			e.currentTarget.checked=false;
        		}else{
        			e.currentTarget.checked=true;
        		}
        		return;
        	}
        	
        	if(e.currentTarget.checked){
    			e.currentTarget.checked=false;
    			var pkDinner = $(e.currentTarget).attr('data-pkDinner');
    			var version = $(e.currentTarget).attr('data-version');
    			
    			var dForm = dinnerForm.initComponent(widget);
            	Dialog.showComponent(dForm, {
                    title : "餐饮准备",
                    confirm : function(){
                    	var formData = dForm.getData();
                    	formData.pkCheckInImplement = that.pkCheckInImplement;
                    	formData.pkCheckInDinner = that.pkCheckInDinner;
                    	formData.pkDinner = pkDinner;
                    	formData.version = version;
                    	formData.isValid = true;
                    	formData.status = "Pending";
                    	
                    	checkinimplementSvc.saveBatchDinner(formData,function(idata){
                    		
                    		widget.pkCheckInDinner = idata.pkCheckInDinner;
                    		that.pkCheckInDinner = idata.pkCheckInDinner;
                    		
                    		that.getDinnersSvc(widget,idata.pkCheckInDinner,function(dinnersData){
            					that.mainData = dinnersData;
            					var tempDate = widget.get("ciiBaseForm").getValue("checkInDate");
            					that.renderCalendar(widget,tempDate);
            					that.setCalendarData(widget,tempDate,that.mainData);
            				});
                    		
                    	});
                    	
                    }
                });
            	
            	var tFormData = that.getDataInMain(pkDinner);
            	if(tFormData){
            		dForm.setData(tFormData);	
            	}
            	var dateOfDinner = $(e.currentTarget).attr('data-dateOfDinner');
            	
            	dForm.setValue("dateOfDinner",moment(dateOfDinner).format("YYYY-MM-DD"));
            	dForm.setValue("dinnerSort",type);
            	dForm.setDisabled("dateOfDinner",true);
            	dForm.setDisabled("dinnerSort",true);
    		}else{
    			var pkDinner = $(e.currentTarget).attr('data-pkDinner');
    			var version = $(e.currentTarget).attr('data-version');
    			e.currentTarget.checked=true;
            	var data = {
            			pkDinner:pkDinner,
            			isValid:false,
            			version:version
            	};
            	checkinimplementSvc.saveDinner(data,function(idata){
            		e.currentTarget.checked=false;
            		
            		that.getDinnersSvc(widget,widget.pkCheckInDinner,function(dinnersData){
    					that.mainData = dinnersData;
    					var tempDate = widget.get("ciiBaseForm").getValue("checkInDate");
    					that.renderCalendar(widget,tempDate);
    					that.setCalendarData(widget,tempDate,that.mainData);
    				});
            		
            	});
    			
    		}
        },
        leftMonth:function(e,widget){
        	tempDate = moment($("#monthStr").text()).subtract(1, "M");
			this.renderCalendar(widget,tempDate);
			this.setCalendarData(widget,tempDate,this.mainData);
        },
        rightMonth:function(e,widget){
        	tempDate = moment($("#monthStr").text()).add(1, "M");
			this.renderCalendar(widget,tempDate);
			this.setCalendarData(widget,tempDate,this.mainData);
        },
        batchReady:function(e,widget){
        	var that = this;
        	var startDate = widget.get("ciiBaseForm").getValue("checkInDate");
        	var endDate = widget.get("ciiBaseForm").getValue("checkOutDate");
        	
        	var dForm = dinnerForm.initComponent(widget,startDate,endDate);
        	Dialog.showComponent(dForm, {
                title : "餐饮批量准备",
                confirm : function(){
                	var formData = dForm.getData();
                	
                	formData.pkCheckInImplement = that.pkCheckInImplement;
                	formData.pkCheckInDinner = that.pkCheckInDinner;
                	formData.startDate = formData.dateOfDinnerRange.start;
                	formData.endDate = formData.dateOfDinnerRange.end;
                	formData.status = "Pending";
                	
                	checkinimplementSvc.saveBatchDinner(formData,function(idata){
                		
                		widget.pkCheckInDinner = idata.pkCheckInDinner;
                		that.pkCheckInDinner = idata.pkCheckInDinner;
                		
                		that.getDinnersSvc(widget,idata.pkCheckInDinner,function(dinnersData){
        					that.mainData = dinnersData;
        					var tempDate = widget.get("ciiBaseForm").getValue("checkInDate");
        					that.renderCalendar(widget,tempDate);
        					that.setCalendarData(widget,tempDate,that.mainData);
        				});
                		
                	});
                }
            });
        },
        getDataInMain:function(pkDinner){
        	this.mainData
        	var idata = null;
        	
        	for(var i=0;i<this.mainData.length;i++){
        		if(pkDinner == this.mainData[i].pkDinner){
        			idata = this.mainData[i];
        			break;
        		}
        	}
        	return idata;
        }
	};
	module.exports = instance;
});
